package com.gougou.search.config;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName RabbitMqConfig
 * @Description TODO
 * @Author c
 * @Date 2018/9/18 19:13
 * @Vcrsion 1.0
 **/
@Configuration
public class RabbitMqConfig {

    // 创建一个连接工厂
    @Bean
    public ConnectionFactory connectionFactory(){
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setAddresses("132.232.50.106:5672");
        connectionFactory.setUsername("gougou");
        connectionFactory.setPassword("gougou");
        connectionFactory.setVirtualHost("/gougou");
        connectionFactory.setPublisherConfirms(true);//消息确认
        connectionFactory.setPublisherReturns(true);
        return connectionFactory;

    }

    // 创建用户管理
    @Bean
    public RabbitAdmin rabbitAdmin(){
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory());
        // 声明队列
        rabbitAdmin.declareQueue(queue());
        return rabbitAdmin;
    }

    // 创建一个队列
    @Bean
    public Queue queue(){
        return new Queue("GOUGOU-SEARCH-ITEM-QUEUE",true, false, false);
    }
}
